package action.admin.account;

import beans.Account;
import dao.CheckAccount;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class changePass extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String old_password = request.getParameter("old_password");
        String new_password1 = request.getParameter("new_password1");
        String new_password2 = request.getParameter("new_password2");
        String acc_id = request.getParameter("acc_id");
        String msg = "";
        String error = "";
        CheckAccount ck = new CheckAccount();
        if (new_password1.equals(new_password2)) {
            Account account = new Account();
            account.setAcc_id(acc_id);
            if (ck.checkPassword(acc_id, old_password)) {
                account.setPass_word(new_password1);
                if (ck.changePassword(account)) {
                    msg = "Password update successful !";
                } else {
                    error = "Password update error! - change again !";
                }
            } else {
                error = "Old Password error!";
            }
        } else {
            error = "Confirm password fail !";
        }
        request.setAttribute("msg", msg);
        request.setAttribute("error", error);
        RequestDispatcher rd = request.getRequestDispatcher("index.jsp?page=changepass");
        rd.forward(request, response);
    }
}
